package view;

import Model.Obj.User;
import Model.OrcUtils.JDBCUtils;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;

public class MainGUI extends JFrame {

    private JPanel contentPane;
    private String name;

    public MainGUI(String name) {
        setTitle("\u7BA1\u7406\u5458");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        JMenu mnNewMenu = new JMenu("\u6570\u636E\u7BA1\u7406");
        menuBar.add(mnNewMenu);

        JMenu mnNewMenu_1 = new JMenu("\u4F1A\u5458\u7BA1\u7406");
        mnNewMenu.add(mnNewMenu_1);

        JMenuItem mntmNewMenuItem = new JMenuItem("\u552E\u5361\u7BA1\u7406");
        mntmNewMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mntmNewMenuItemActionPerformed(e);
            }
        });
        mnNewMenu_1.add(mntmNewMenuItem);

        JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u4F1A\u5458\u8D44\u6599\u7BA1\u7406");
        mntmNewMenuItem_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mntmNewMenuItem_1ActionPerformed(e);
            }
        });
        mnNewMenu_1.add(mntmNewMenuItem_1);

        JMenuItem mntmNewMenuItem_6 = new JMenuItem("\u6D88\u8D39\u7BA1\u7406");
        mntmNewMenuItem_6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mntmNewMenuItem_6ActionPerformed(e);
            }
        });

        JMenuItem mntmNewMenuItem_3 = new JMenuItem("\u5361\u7BA1\u7406");
        mntmNewMenuItem_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mntmNewMenuItem_3ActionPerformed(e);
            }
        });
        mnNewMenu.add(mntmNewMenuItem_3);
        mnNewMenu.add(mntmNewMenuItem_6);

        JMenuItem mntmNewMenuItem_2 = new JMenuItem("\u9000\u51FA\u767B\u5F55");
        mntmNewMenuItem_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mntmNewMenuItem_2ActionPerformed(e);
            }
        });
        mnNewMenu.add(mntmNewMenuItem_2);

        JMenu mnNewMenu_4 = new JMenu("\u7CFB\u7EDF\u7BA1\u7406");
        menuBar.add(mnNewMenu_4);

        JMenuItem mntmNewMenuItem_7 = new JMenuItem("导出报表");
        mntmNewMenuItem_7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mntmNewMenuItem_7ActionPerformed(e);
            }
        });
        mnNewMenu_4.add(mntmNewMenuItem_7);

        JMenuItem mntmNewMenuItem_8 = new JMenuItem("\u6743\u9650\u8BBE\u7F6E");
        mntmNewMenuItem_8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mntmNewMenuItem_8ActionPerformed(e);
            }
        });
        mnNewMenu_4.add(mntmNewMenuItem_8);

        JMenuItem mntmNewMenuItem_9 = new JMenuItem("\u66F4\u6539\u5BC6\u7801");
        mntmNewMenuItem_9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                mntmNewMenuItem_9ActionPerformed(e);
            }
        });
        mnNewMenu_4.add(mntmNewMenuItem_9);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        this.name = name;
    }
    //卡管理
    private void mntmNewMenuItem_3ActionPerformed(ActionEvent e) {
        new CardGUI().setVisible(true);
    }
    //更改密码
    private void mntmNewMenuItem_9ActionPerformed(ActionEvent e) {
        new PasswordChange(name).setVisible(true);
    }
    //权限设置
    private void mntmNewMenuItem_8ActionPerformed(ActionEvent e) {
        String sql = "select * from USERDATA where USERNAME=?";
        List<User> list= JDBCUtils.JDBCselect(sql,User.class,name);
        for (User user : list){
            if (user.getPOWER().equals("超级管理员")){
                new PowerGUI().setVisible(true);
            } else {
                JOptionPane.showMessageDialog(null,"您的权限不足");
                return;
            }
        }
    }
    //导出报表
    private void mntmNewMenuItem_7ActionPerformed(ActionEvent e) {
        new CSVGUI().setVisible(true);
    }
    //消费管理
    private void mntmNewMenuItem_6ActionPerformed(ActionEvent e) {
        new SonsumeGUI().setVisible(true);
    }
    //会员资料管理
    private void mntmNewMenuItem_1ActionPerformed(ActionEvent e) {
        new MemberGUI().setVisible(true);
    }
    //售卡管理
    private void mntmNewMenuItemActionPerformed(ActionEvent e) {
        new SellGUI().setVisible(true);
    }
    //退出登录
    private void mntmNewMenuItem_2ActionPerformed(ActionEvent e) {
        this.dispose();
        new LoginGUI().setVisible(true);
    }

}
